package com.higgins.eventtracking.client.health.overview;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HTMLTable.Cell;
import com.google.gwt.user.client.ui.Label;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.dto.GoalDTO;
import com.higgins.eventtracking.client.dto.HealthAlertDTO;
import com.higgins.eventtracking.client.dto.SupportDTO;
import com.higgins.eventtracking.client.dto.TimeTableTaskDTO;
import com.higgins.eventtracking.client.genericcomponents.GenericColumn;
import com.higgins.eventtracking.client.genericcomponents.GenericColumns;
import com.higgins.eventtracking.client.genericcomponents.GenericTable;

public class SupportTable extends GenericTable	{
	private static final Logger log = Logger.getLogger(SupportTable.class.getName());
	private final EventTrackingServiceAsync greetingService = GWT
			.create(EventTrackingService.class);
	private String frameOfMind = null;
	
	public SupportTable(GenericColumns genericColumns) {
		super(genericColumns);
		listsLoaded();
	}
	public void listsLoaded()	{
		retrieveDTOs();
	}
	public void retrieveDTOs()	{
		greetingService.retrieveSupport(new AsyncCallback<List<SupportDTO>>()  {
			@Override
			public void onFailure(Throwable caught) {
				log.info("TimeTableTable: " + caught.toString());
			}

			@Override
			public void onSuccess(List<SupportDTO> result) {
				loadData(convertDTOtoRowArray(result));
			}
		});		
	}
	private List<List<Object>> convertDTOtoRowArray(List<SupportDTO> dtos)	{
		List<List<Object>> rows = new ArrayList<List<Object>>();
		for (SupportDTO dto:dtos)	{
			List<Object> row = new ArrayList<Object>();
			row.add(dto.getId());
			row.add(dto.getLevel()!=null?dto.getLevel():1);
			row.add(dto.getName()!=null?dto.getName():"");
			row.add(dto.getEmail()!=null?dto.getEmail():"");
			row.add(dto.getPhone()!=null?dto.getPhone():"");
			rows.add(row);
		}
		return rows;
	}
	protected void addObject(int row, int column, Object object)	{
		ensureRowAvailable(row);
		GenericColumn genericColumn = genericColumns.getColumn(column);
//		if (genericColumn.getType().equals(JobSiteDTO.class))	{
//			addJobSiteDTO(row, column, object, genericColumn);
//		if (column==3)	{
//			addLabel(row, column, object, genericColumn);
//		}
//		if (column==4)	{
//			addButton(row, column, object, genericColumn);
//		}
	}
/*	
	public void addLabel(int row, int column, Object object, GenericColumn genericColumn)	{
		Label newLabel = new Label("Up");
		setWidget(row, column, newLabel);
		getCellFormatter().addStyleName(row, column, "FlexTable-normCell");
	}
	*/
		
	public void updateItemCell(int row)	{
		updateItemCell(row, 0);
	}

	public void updateItemCell(int row, int move)	{
		SupportDTO dto = new SupportDTO();
		dto.setId(getId(row));
		dto.setLevel(getInteger(row, 1));
		dto.setName(getText(row, 2));
		dto.setEmail(getText(row, 3));
		dto.setPhone(getText(row, 4));
		updateRow(dto);
	}
	
	private void updateRow(SupportDTO dto)	{
		try {
			greetingService.updateSupport(dto, new AsyncCallback<String>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("HealthAlertTable: " + caught.toString());
					}

					@Override
					public void onSuccess(String result) {
						setRowIdAfterUpdate(result);
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("HealthAlertTable: " + e.toString());
		} /*catch (SecurityAccessDeniedException e) {
			log.info("SecurityAccessDeniedException: " + e.toString());
		} 		*/
	}
	protected void deleteRow(Long id)	{
		try	{
			greetingService.deleteSupport(id, new AsyncCallback<Long>()   {
				@Override
				public void onFailure(Throwable caught) {
					log.info("GoalTable: Delete Goal Failed!");
				}

				@Override
				public void onSuccess(Long id) {
					refresh();
				}  
			});
		} catch (Exception e)	{
			log.info("GoalTable: " + e.toString());
		}
	}
	protected void makeCellEditable(GenericColumn genericColumn, final int row, final int column)	{
		if (column==3 && row > 1)	{
			updateItemCell(row, -1);
			updateItemCell(row-1, +1);
			refresh();
		}
	}
	
}
